﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhoneApplicPersons.Interfaces;
using PhoneApplicPersons.Classes;

namespace PhoneApplicPersons.DS
{
    /// <summary>
    /// Класс-наследник интерфейса фабрики
    /// </summary>
    public class DS_MOCK : IDS
    {
        private string path;
        public string Path { set { path = value; } }
        private IDS next = null;
        public IDS Next { set { next = value; } }
        public DS_MOCK()
        {
            per = new List<Persona>();
        }
        public DS_MOCK(string _path) 
        { 
            Path = _path;
            per = new List<Persona>();
        }
        public IDS isReady(string _ext, string _path)
        {
            IDS ret = null;
            if ("mock" == _ext) { ret = this; ret.Path = _path; }
            else if (next != null) { ret = next.isReady(_ext, _path); }
            return ret;
        }
        public List<Persona> per = null;

        public void Create(Persona p)
        {
            per.Add(p);
        }

        public List<Persona> Read()
        {
            return per;
        }

        public List<Persona> Read(int ID)
        {
            List<Persona> list = new List<Persona>();
            foreach (Persona p in per)
            {
                if (p.ID == ID)
                {
                    list.Add(p);
                }
            }

            return list;
        }

        public void Update(Persona p)
        {
            try
            {
                int index = GetIndexOf(p.ID);
                per[index].Fname = p.Fname;
                per[index].Lname = p.Lname;
                per[index].Age = p.Age;
            }
            catch (Exception ex)
            {
                throw new SystemException(ex.Message);
            }
        }

        private int GetIndexOf(int ID)
        {
            int index = 0;
            for (int i=0;i<per.Count;i++)
            {
                if (per[i].ID == ID)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }

        public void Delete(int ID)
        {
            try
            {
                per.RemoveAt(GetIndexOf(ID));
            }
            catch (Exception ex)
            {
                throw new SystemException(ex.Message);
            }

        }
    }
}
